<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
	<title>[[${config.systemTitle}]]</title>
    <link rel="shortcut icon" th:href="@{'config-resource/yhfin-resource/'+${config.faviconIcoPath}}" type="image/x-icon">
    <script type="text/javascript" th:src="@{js/jquery-2.1.1.min.js}"></script>
    <script type="text/javascript" th:src="@{js/security.js}"></script>
    <script type="text/javascript" th:src="@{js/json2.js}"></script>
    <script type="text/javascript" th:src="@{js/plugins/layer/layer.min.js}"></script>
    
	<link rel="stylesheet" th:href="@{css/bootstrap.min.css}" type="text/css"/>
	<link rel="stylesheet" th:href="@{css/font-awesome.min.css}" type="text/css"/>
	<link rel="stylesheet" th:href="@{css/login/login.css}" type="text/css"/>
	<link rel="stylesheet" th:href="@{css/login/css.css}" type="text/css"/>
	<link rel="stylesheet" th:href="@{css/login/min.css}" type="text/css"/>
	<style type="text/css">
		.bg {
			background-color: #2e363f;
		    background-position: 50% 50%;
		    background-repeat: no-repeat;
		    background-size: cover;
		    background-image:url(config-resource/yhfin-resource/[[${config.loginBackgroundPath}]]);
		    bottom: 0;
		    height: 100%;
		    left: 0;
		    position: absolute;
		    right: 0;
		    top: 0;
		    width: 100%;
			
		}
	</style>
</head>

<body class="signin" style="background-size: cover;height:80%">
    <div id="background" class="bg" background-image="" >
		<!-- <img src="?" id="backgroudImg" class = "background-img" /> -->
	</div> 
	<div id="loginbox" style="padding-top: 20%;">
		<form id="loginform" name="loginform" class="form-vertical"
			action="loginCheck"
			method="post">
			<div class="control-group normal_text">
				<table style="width: 100%">
					<tr>
						<td align="center" style="font-weight: bold;color: gray;">
							<span>[[${config.systemName}]]</span>
						</td>
					</tr>
				</table>
			</div>
			<div class="control-group">
				<div class="controls">
					<div class="main_input_box">
						<span class="add-on bg_ly" style="background: #28b779">
							<img src="css/login/account.png" alt="请输入账号..">
						</span>						
						<input id="txusername" type="text" placeholder="请输入用户名" name="username" value="" 
							style="height: 37px; margin-bottom: 0px;color:#333;"/>
					</div>
				</div>
			</div>
			<div class="control-group">
				<div class="controls">
					<div class="main_input_box">
						<span class="add-on bg_ly">
							<img src="css/login/lock.png" alt="请输入密码..">
						</span>
						<input id="txpassword" type="password" placeholder="请输入密码" name="password" value=""
							style="height: 37px; margin-bottom: 0px;color:#333;"/>
					</div>
				</div>
			</div>
			 <div style="width: 86%; height:60px;padding-left: 18%;">
				<span class="pull-left">
					<a id="btnlogin" href="#" class="btn btn-success">登&nbsp;&nbsp;录</a>
				</span>
				<span class="pull-right"  style="padding-right: 10%; ">
					<a href="#" class="flip-link btn btn-info" id="to-recover">重&nbsp;&nbsp;置</a>
				</span>
			</div> 
		</form>
	</div>
	
	
	 <script type="text/javascript">
	 var enterClick=true;
     	$(function(){
     		loginSubmit();
			 $(document).keydown(function(event){
		        if(event.keyCode == 13){
		        	if(enterClick){
		        		$('#btnlogin').trigger("click");
		        	}else{
		        		$('.layui-layer-btn0').trigger("click");
		        	}
		        } 
			});
			reset();
			
			//检查license证书
			licenseCheck();
     	});
     	
     	// 判断license有效时间
        function licenseCheck(){
        	$.ajax({
                type : "POST",
                url : "licenseCheck",
                success : function(data) {
                	console.info('返回消息1：' + data);
                	var ret = JSON.parse(data);
                	if(ret.status === 'SUCC'){
                		//layer.msg("license检查成功！");
                		return ;
                	}
                	enterClick=false;
                	layer.open({
              		  type: 1 //Page层类型
              		  ,area: ['auto', 'auto']
              		  ,title: '提示信息'
              		  ,anim: 1 //0-6的动画形式，-1不开启
              		  ,content: '<div style="padding:20px;"><h5>'+ret.msg+
              		  '</h5><textarea id="licenseCode" class="form-control" cols="60" rows="3"></textarea></div>'
              		,btn:['确认','取消']
              		,yes: function(index, layero){
              			var licenseCode=$("#licenseCode").val();
              			licenseCode=licenseCode.trim();
              			if(null==licenseCode || ''==licenseCode){
              				layer.msg("license不能为空！");
              				return ;
              			}
              			$.ajax({
              		    	type:"POST",
              		    	url : "insertLicense",
              		    	data: {licenseCode:licenseCode},
              		    	success : function(data) {
              		    		console.info('返回消息2：' + data);
              	            	var ret = JSON.parse(data);
              	            	if(ret.status === 'SUCC'){
              	            		layer.msg("license证书插入成功！");
              	            		layer.close(index);
              	            		enterClick=true;
              	            	}else{
              	            		layer.msg("license证书插入失败！");
              	            	}
              		    	},
              		    	error:function(data){
              	            	layer.msg("license证书插入失败！");
              	            }
              		    });
              		  }
              		,btn2: function(index, layero){
              			layer.close(index);
              			enterClick=true;
              		}
              		});
                },
                error:function(data){
                	layer.msg("license证书检查失败！");
                }
            });
        }
     	
     	function isNull(str){
     		if(str === null || str === "" || str === undefined){
     			return true;
     		}
     		return false;
     	}
     	
     	/**
     	 * 密码加密
     	 */
     	function encrypt(password) {
			var modulus = "00f428e2d928aa802b0ad018a895a4d913f1d22eeacc464eca7ecb480d877001911992d863d29045a56aef761caffc80f88157e375341f84b573eeab2dcb33431dcb5ea973394577d210603246c030f1b529869333e86117f3a81e3faa1c419844b7b5bc76d91dfa1861343f3e7064c18d1e1673d23f625f56e4095f816fc39b8d";
			var exponent = "010001";
			var key = RSAUtils.getKeyPair(exponent, '', modulus);
			var pwd = RSAUtils.encryptedString(key, password);
			console.info("加密后密码：" + pwd);
			return pwd;
		}
     	
     	function reset(){
     		$('#to-recover').on('click',function(){
    			 $("#txusername").val('');
                $("#txpassword").val('');
    		});
     	}
     	
     	function loginSubmit(){
     		$('#btnlogin').on('click',function(){
     			//检测license证书
     			$.ajax({
                    type : "POST",
                    url : "licenseCheck",
                    success : function(data) {
                    	console.info('返回消息1：' + data);
                    	var ret = JSON.parse(data);
                    	if(ret.status === 'SUCC'){
                    		//layer.msg("license检查成功！");
                    		loginCheck();  //登录
                    	}else{
                    		enterClick=false;
                    	layer.open({
                  		  type: 1 //Page层类型
                  		  ,area: ['auto', 'auto']
                  		  ,title: '提示信息'
                  		  ,anim: 1 //0-6的动画形式，-1不开启
                  		  ,content: '<div style="padding:20px;"><h5>'+ret.msg+
                  		  '</h5><textarea id="licenseCode" class="form-control" cols="60" rows="3"></textarea></div>'
                  		,btn:['确认','取消']
                  		,yes: function(index, layero){
                  			var licenseCode=$("#licenseCode").val();
                  			licenseCode=licenseCode.trim();
                  			if(null==licenseCode || ''==licenseCode){
                  				layer.msg("license不能为空！");
                  				return ;
                  			}
                  			$.ajax({
                  		    	type:"POST",
                  		    	url : "insertLicense",
                  		    	data: {licenseCode:licenseCode},
                  		    	success : function(data) {
                  		    		console.info('返回消息2：' + data);
                  	            	var ret = JSON.parse(data);
                  	            	if(ret.status === 'SUCC'){
                  	            		layer.msg("license证书更新成功！");
                  	            		layer.close(index);
                  	            		enterClick=true;
                  	            		loginCheck(); //登录
                  	            	}else{
                  	            		layer.msg("license证书更新失败！");
                  	            	}
                  		    	},
                  		    	error:function(data){
                  	            	layer.msg("license证书更新失败！");
                  	            }
                  		    });
                  		  }
                  		,btn2: function(index, layero){
                  			layer.close(index);
                  			enterClick=true;
                  		}
                  		});
                    }
                    },
                    error:function(data){
                    	layer.msg("license证书检查失败,请联系软件开发商进行处理！");
                    }
                });
     			
     		});
     	}
     	
     	function loginCheck(){
     		var username = $("#txusername").val();
            var password = $("#txpassword").val();
            if(isNull(username)){
               	layer.msg('用户名不能为空！'); 
               	 return ;
            }
            if(isNull(password)){
               	layer.msg('密码不能为空！'); 
               	 return ;
            }
            var encryptPwd = encrypt(password);
            console.info('加密后密码' + encryptPwd);
            var params = {
           		 username:username,
           		 password:encryptPwd
            };
 			console.info('请求数据' + params);
 			$.ajax({
                type : "POST",
                url : "loginCheck",
                data : params,
                success : function(data) {
                	console.info('返回消息：' + data);
                	var ret = JSON.parse(data);
                	if(ret.status === 'SUCC'){
                        var scr = document.createElement("iframe");      //创建iframe 
                        var configFineReportUrl = "[[${config.fineReportUrl}]]";
                        var fineReportUrl = 'http://'+configFineReportUrl+'?op=fs_load&cmd=sso&fr_username='+username+'&fr_password='+password;
                        scr.src = fineReportUrl;   //将报表验证用户名密码的地址指向此iframe
                        if (scr.attachEvent) {       //判断是否为ie浏览器    
                               scr.attachEvent("onload", function(){                    //如果为ie浏览器则页面加载完成后立即执行    
                                   //window.location="https:///192.168.1.40:8075/webroot/decision"; //直接跳转到数据决策系统  
                               });    
                           } else {    
                              scr.onload = function(){              //其他浏览器则重新加载onload事件    
                              //window.location="https:///192.168.1.40:8075/webroot/decision"; //直接跳转到数据决策系统  
                           };    
                        }    
                        document.getElementsByTagName("head")[0].appendChild(scr);    //将iframe标签嵌入到head中 
                        parent.document.location.href = "index";
                	}else{
                		console.info('返回错误消息：' + ret.msg);
                		layer.msg(ret.msg); 
                	}
                },
                error:function(data){
					
                }
            });
     	}
     </script>
</body>

</html>